<template>
  <a-upload
    name="imageFile"
    :multiple="true"
    accept=".txt"
    :data="paramsData"
    action="/yzm-web-api/image/upload"
    @change="handleChange"
  >
    <a-button type="primary" :loading="confirmLoading"> <a-icon type="download" /> <slot></slot> </a-button>
  </a-upload>
</template>
<script>
import { mapActions } from 'vuex'
import utils from '@/utils/common'
var timeStamp = Number(new Date())
export default {
  data () {
    return {
      paramsData: {
        token: utils.AuthToken(timeStamp, this.$store.getters.userKey),
        timestamp: timeStamp,
        adminId: this.$store.getters.adminId,
        imageType: 4
      },
      confirmLoading: false
    }
  },
  methods: {
    ...mapActions(['SensitiveImport']),
    handleChange (info) {
      this.confirmLoading = true
      if (info.file.status !== 'uploading') {
      }
      if (info.file.status === 'done') {
        this.ApiImport(info.file.response.data.imageUrl)
      } else if (info.file.status === 'error') {
        this.$message.error('导入出现错误，请检查模板')
      }
    },
    ApiImport (file) {
      this.SensitiveImport({ fileUrl: file })
        .then((res) => this.delSuccess(res))
        .catch(err => this.requestFailed(err))
    },
    delSuccess (res) {
      if (res.status === 200 || res.status === '200') {
        this.$notification['success']({
          message: '成功',
          description: '导入成功',
          duration: 4
        })
        this.$emit('modelTxt')
        setTimeout(() => {
          this.confirmLoading = false
        }, 1000)
      } else {
        this.requestFailed(res)
      }
    },
    requestFailed (err) {
      this.$notification['error']({
        message: '错误',
        description: err.message || '请求出现错误，请稍后再试',
        duration: 4
      })
      this.confirmLoading = false
    }
  }
}
</script>
